<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="集装箱号" prop="containerCode">
        <el-input
          v-model="queryParams.containerCode"
          placeholder="请输入集装箱号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="铅封号" prop="sealNumber">
        <el-input
          v-model="queryParams.sealNumber"
          placeholder="请输入铅封号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际进港时间" prop="actualArrivalTime">
        <el-date-picker clearable
          v-model="queryParams.actualArrivalTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际进港时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:container:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:container:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:container:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:container:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="containerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="集装箱ID" align="center" prop="orderContainerId" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="集装箱号" align="center" prop="containerCode" />
      <el-table-column label="铅封号" align="center" prop="sealNumber" />
      <el-table-column label="箱皮重" align="center" prop="tareWeight" />
      <el-table-column label="箱型" align="center" prop="containerType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_order_container_type" :value="scope.row.containerType"/>
        </template>
      </el-table-column>
      <el-table-column label="总体积" align="center" prop="totalVolume" />
      <el-table-column label="总质量" align="center" prop="totalMass" />
      <el-table-column label="件数" align="center" prop="objectNumber" />
      <el-table-column label="提箱地点" align="center" prop="pickupLocation" />
      <el-table-column label="实际进港时间" align="center" prop="actualArrivalDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualArrivalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"  width="120px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:container:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:container:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单-集装箱信息对话框 -->
    <el-dialog :title="title" :visible.sync="open"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-col :span="8">
              <el-form-item label="订单ID" prop="orderId">
                <el-input v-model="form.orderId" placeholder="请输入订单ID" :disabled="isDisabled"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="订单号" prop="orderCode" >
                <el-input v-model="form.orderCode" placeholder="请输入订单号" :disabled="isDisabled"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="集装箱号" prop="containerCode">
                <el-input v-model="form.containerCode" placeholder="请输入集装箱号" />
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="24">
            <el-col :span="8">
              <el-form-item label="铅封号" prop="sealNumber">
                <el-input v-model="form.sealNumber" placeholder="请输入铅封号" />
              </el-form-item>
            </el-col>
            <el-col :span="8">

              <el-form-item label="箱皮重" prop="tareWeight">
                <el-input v-model="form.tareWeight" placeholder="请输入箱皮重" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="总体积" prop="totalVolume">
                <el-input v-model="form.totalVolume" placeholder="请输入总体积" />
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="24">
            <el-col :span="8">
              <el-form-item label="总质量" prop="totalMass">
                <el-input v-model="form.totalMass" placeholder="请输入总质量" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="件数" prop="objectNumber">
                <el-input v-model="form.objectNumber" placeholder="请输入件数" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="提箱地点" prop="pickupLocation">
                <el-input v-model="form.pickupLocation" placeholder="请输入提箱地点" />
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="24">
            <el-col :span="8">
              <el-form-item label="实际进港时间" prop="actualArrivalDate" >
                <el-date-picker clearable
                                v-model="form.actualArrivalDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择实际进港时间"
                                style="width: 140px">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContainer, getContainer, delContainer, addContainer, updateContainer } from "@/api/system/order/container";

export default {
  name: "Container",
  dicts: [ 'sys_order_container_type'],
  data() {
    return {
      isDisabled:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单-集装箱信息表格数据
      containerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderCode: null,
        containerCode: null,
        sealNumber: null,
        tareWeight: null,
        containerType: null,
        totalVolume: null,
        totalMass: null,
        objectNumber: null,
        pickupLocation: null,
        actualArrivalTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
        orderCode: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        containerCode: [
          { required: true, message: "集装箱号不能为空", trigger: "blur" }
        ],
        sealNumber: [
          { required: true, message: "铅封号不能为空", trigger: "blur" }
        ],
        tareWeight: [
          { required: true, message: "箱皮重不能为空", trigger: "blur" }
        ],
        // containerType: [
        //   { required: true, message: "箱型不能为空", trigger: "change" }
        // ],
        // totalVolume: [
        //   { required: true, message: "总体积不能为空", trigger: "blur" }
        // ],
        // totalMass: [
        //   { required: true, message: "总质量不能为空", trigger: "blur" }
        // ],
        objectNumber: [
          { required: true, message: "件数不能为空", trigger: "blur" }
        ],
        // pickupLocation: [
        //   { required: true, message: "提箱地点不能为空", trigger: "blur" }
        // ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单-集装箱信息列表 */
    getList() {
      this.loading = true;
      listContainer(this.queryParams).then(response => {
        this.containerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderContainerId: null,
        orderId: null,
        orderCode: null,
        containerCode: null,
        sealNumber: null,
        tareWeight: null,
        containerType: null,
        totalVolume: null,
        totalMass: null,
        objectNumber: null,
        pickupLocation: null,
        actualArrivalTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderContainerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单-集装箱信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderContainerId = row.orderContainerId || this.ids
      getContainer(orderContainerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单-集装箱信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderContainerId != null) {
            updateContainer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContainer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderContainerIds = row.orderContainerId || this.ids;
      this.$modal.confirm('是否确认删除订单-集装箱信息编号为"' + orderContainerIds + '"的数据项？').then(function() {
        return delContainer(orderContainerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/container/export', {
        ...this.queryParams
      }, `container_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
